package com.lenovo.lcr.sil.wangdwa.learning;


import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ListActivityDemoActivity extends ListActivity {
	
	// used as the keys
	private String[] mColumnNames = {
			"Name",	
			"Sex",	
			"Age"	
	};
	private final int[] mViewIds = {
			R.id.TXT_NAME,
			R.id.TXT_SEX,
			R.id.TXT_AGE
	};
	
	private ArrayList<HashMap<String, String>> mItems = null;
	
	private TextView mTxtFooter = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Initialize the items in the table
        mItems = new ArrayList<HashMap<String, String>>();
        addItem("Paul Wang", "Male", "29");
        addItem("Leon Tang", "Female", "27");
        addItem("Jack Chou", "Male", "30");
        addItem("Jerry Chen", "Female", "28");
        
        // 
        ListAdapter adapter = new SimpleAdapter(this, mItems, R.layout.row_ui, mColumnNames, mViewIds);
        
        this.getListView().addHeaderView(this.getLayoutInflater().inflate(R.layout.header_view, null), null, false);
        this.getListView().addFooterView(this.getLayoutInflater().inflate(R.layout.footer_view, null), null, false);
        
        
        setListAdapter(adapter);
        
        mTxtFooter = (TextView)findViewById(R.id.TXT_FOOTER);
    }

	private void addItem(String name, String sex, String age) {
		// TODO Auto-generated method stub
		HashMap<String, String> item = new HashMap<String, String>();
		item.put(mColumnNames[0], name);
		item.put(mColumnNames[1], sex);
		item.put(mColumnNames[2], age);
		mItems.add(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		
		HashMap<String, String> item = new HashMap<String, String>(mItems.get(position-1));
		String hint = "Current item is: " 
				+ item.get(mColumnNames[0]).toString() + " " 
				+ item.get(mColumnNames[1]).toString() + " " 
				+ item.get(mColumnNames[2]).toString();
		
		Toast.makeText(this, hint, Toast.LENGTH_LONG).show();
		
		mTxtFooter.setText(hint);
		
	}
    
    
    
}